package com.power.using.mydaggerp.model.home.tabs;

import com.power.using.mydaggerp.api.WangyiApi;
import com.power.using.mydaggerp.contract.home.tabs.WangyiContract;
import com.power.using.mydaggerp.model.bean.wangyi.WangyiNewsListBean;
import com.zyw.horrarndoo.sdk.config.DBConfig;
import com.zyw.horrarndoo.sdk.config.ItemState;
import com.zyw.horrarndoo.sdk.helper.RetrofitCreateHelper;
import com.zyw.horrarndoo.sdk.helper.RxHelper;
import com.zyw.horrarndoo.sdk.utils.AppUtils;
import com.zyw.horrarndoo.sdk.utils.DBUtils;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * Created by 聪明一只哈 on 2019/10/11.
 * Time: 2019/10/11  14:23
 */
public class WangyiModel implements WangyiContract.IWangyiModel {


    public static WangyiModel newInstance(){
        return new WangyiModel();
    }


    @Override
    public Observable<WangyiNewsListBean> getNewsList(int id) {
        return RetrofitCreateHelper.createApi(WangyiApi.class,WangyiApi.HOST).getNewsList(id)
                .compose(RxHelper.<WangyiNewsListBean>rxSchedulerHelper());
    }

    @Override
    public Observable<Boolean> recordItemIsRead(final String key) {
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {

                emitter.onNext(DBUtils.getDB(AppUtils.getContext())
                    .insertRead(DBConfig.TABLE_WANGYI,key, ItemState.STATE_IS_READ));

                emitter.onComplete();
            }
        }).compose(RxHelper.<Boolean>rxSchedulerHelper());
    }
}
